package net.chenyan.server.controller.admapi;/**
 * @author chenyan
 * @createTime 2022/3/24 15:18
 * @description description
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.chenyan.commons.core.R;

import net.chenyan.server.config.AppConfig;
import net.chenyan.server.dto.UserAddDTO;
import net.chenyan.server.dto.UserLoginDTO;
import net.chenyan.server.dto.UserUpdateDTO;
import net.chenyan.server.po.User;
import net.chenyan.server.service.UserService;
import net.chenyan.server.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 *@description: TODO
 *@author: chenyan
 *@create: 2022/3/24 15:18
 */
@Slf4j
@Api(tags = {"用户个人信息接口"})
@RestController
@RequestMapping(value = "/admapi/user")
public class UserController {
        @Autowired
        private UserService userService;


        @ApiOperation(value = "获取用户个人信息")
        @GetMapping(value = "/get")
        public R getInfo( int userId) {
            if(userId==0){
                return R.error("请提供用户id");
            }
            User user = userService.getById(userId);
            if (user == null) {
                return R.error("数据库根据id找不到这个用户？");
            }
            UserVo userVo=new UserVo();
            BeanUtils.copyProperties(userVo, user);
            return R.data(userVo);
        }


        @ApiOperation(value = "修改个人信息")
        @PutMapping(value = "/update")
        public R update(@RequestBody UserUpdateDTO userUpdateDTO) {
            User user1 = userService.getById(userUpdateDTO.getId());
            if(user1==null){
                return R.error("该用户不存在，id错误!");
            }
            user1.setName(userUpdateDTO.getName());
            user1.setPassword(userUpdateDTO.getPassword());
            user1.setPortrait(userUpdateDTO.getPortrait());
            user1.setSignature(userUpdateDTO.getSignature());
            boolean temp = userService.updateById(user1);
            return temp ? R.ok("修改成功") : R.error("修改失败");
        }
}
